#!/bin/bash

alias mv='mv' # 取消强制覆盖时的确认
alias echo='echo -e'
QUOTA="/${QUOTA_TEST}/quota"
QUOTADIR="${FS_MOUNTPOINT}${QUOTA}"
global_used=0

todir="$QUOTADIR/todir"
fromdir="$QUOTADIR/fromdir"

msg0="测试所有用例"
msg1="测试在 <同一> 配额目录下 mv 源目录为 <不存在> 的目标目录 (不超出配额)"
msg2="测试在 <同一> 配额目录下 mv 源目录到 <存在> 的目标目录, 源目录变为目标目录的子目录 (不超出配额)"
msg3="测试在 <同一> 配额目录下 mv 源目录到 <存在> 的目标目录, 目标目录存在与源目录同名的子目录(报错:不允许mv)"
msg4="测试在 <同一> 配额目录下 mv 源目录到 <存在> 的目标目录, 目标目录存在与源目录同名的子目录(加-b参数强制覆盖且旧目录生成备份)"
msg5="测试在 <同一> 配额目录下 mv 源目录到 <存在> 的文件 (vfs 直接报错)"
maxmsg=5

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh test_one.sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function reset_all() {
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    clean_all_dir;
    
    orcafs-ctl --setquota --dir=${QUOTADIR} --hardlimit=$1M &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    echo -n "=====清空目录并重新创建配额目录 ${QUOTA} (硬阈值$1MB)====="
    quota_num=`orcafs-ctl --listquota | grep -w DIR | wc -l`
    if ((${quota_num} == 1));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function clean_all_dir() {
    mkdir -p ${QUOTADIR}; func_cleandir ${QUOTADIR}
}

function func_get_rename_quota_used() {
    quota_name=$1
    used=`orcafs-ctl --listquota | grep -E -w ${quota_name} | grep -w DIR | awk '{print $8}'`
    #echo -n -e "quota used (${used}):\n  [ls ${WRITEDIR}]: "; ls -l ${WRITEDIR}
    echo "\t${quota_name} quota used (${used})"
    global_used=$used
}

function rename_from_childdir_to_nonexistdir() {
    clean_all_dir
    file1_size=$(($1 * 1024 * 1024))
    file2_size=$(($2 * 1024 * 1024))
    quota_dir_size=$((${file1_size} + ${file2_size}))
    
    mkdir -p ${fromdir}
    # mkdir -p ${todir}  # 目标目录不存在
    cmd="dd if=/dev/zero of=${fromdir}/file1 bs=${file1_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=${fromdir}/file2 bs=${file2_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    func_get_rename_quota_used ${QUOTA}; old_used=${global_used}
    echo -n -e "\tbefore mv:\n\t\t"; ls $QUOTADIR
    cmd="mv ${fromdir} ${todir}";echo "\tExec: ${cmd}"; eval ${cmd}
    echo -n -e "\tafter mv:\n\t\t"; ls $QUOTADIR
    func_get_rename_quota_used ${QUOTA}; new_used=${global_used}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($old_used == $quota_dir_size)) && (($new_used == $old_used)) 
        then echo "success\n"; else echo "fail\n"; exit 1; fi
}

function rename_from_childdir_to_existdir() {
    clean_all_dir
    file1_size=$(($1 * 1024 * 1024))
    file2_size=$(($2 * 1024 * 1024))
    quota_dir_size=$((${file1_size} + ${file2_size}))
    
    mkdir -p ${fromdir}
    mkdir -p ${todir} # 目标目录存在
    cmd="dd if=/dev/zero of=${fromdir}/file1 bs=${file1_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=${fromdir}/file2 bs=${file2_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    func_get_rename_quota_used ${QUOTA}; old_used=${global_used}
    echo -n "\tbefore mv (ls $QUOTADIR):\n\t\t"; ls $QUOTADIR
    cmd="mv ${fromdir} ${todir}";echo "\tExec: ${cmd}"; eval ${cmd}
    echo -n "\tafter mv (ls $todir):\n\t\t"; ls $todir
    func_get_rename_quota_used ${QUOTA}; new_used=${global_used}

    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($old_used == $quota_dir_size)) && (($new_used == $old_used)) 
        then echo "success\n"; else echo "fail\n"; exit 1; fi
}

function rename_from_srcdir_to_destdir_includesrc() {
    clean_all_dir
    file_size=$((1 * 1024 * 1024))
    
    mkdir -p ${fromdir}
    mkdir -p ${todir}/fromdir # 目标目录存在与源目录同名的子目录
    cmd="dd if=/dev/zero of=${fromdir}/file1 bs=${file_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    echo -n "\tbefore mv (ls $QUOTADIR):\t"; ls $QUOTADIR
    echo -n "\tbefore mv (ls $todir):\t"; ls $todir
    
    cmd="mv ${fromdir} ${todir}";echo "\tExec: ${cmd}"; eval ${cmd}; res=$?
    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($res == 1)); then echo "success\n"; else echo "fail\n"; exit 1; fi
}

function rename_from_srcdir_to_destdir_includesrc_backup() {
    clean_all_dir
    
    mkdir -p ${fromdir}
    mkdir -p ${todir}/fromdir # 目标目录存在与源目录同名的子目录

    file_size=$((1 * 1024 * 1024))
    cmd="dd if=/dev/zero of=${fromdir}/file bs=${file_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=${todir}/fromdir/file2 bs=${file_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    echo -n "\tbefore mv (ls $QUOTADIR):\t"; ls $QUOTADIR
    echo -n "\tbefore mv (ls $todir):\t"; ls $todir
    
    cmd="mv -b ${fromdir} ${todir}";echo "\tExec: ${cmd}"; eval ${cmd}; res=$?
    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($res == 1)); then echo "success\n"; else echo "fail\n"; exit 1; fi
}

function rename_from_srcdir_to_file() {
    clean_all_dir
    mkdir -p ${fromdir}

    file_size=$((1 * 1024 * 1024))
    cmd="dd if=/dev/zero of=${fromdir}/file1 bs=${file_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null
    cmd="dd if=/dev/zero of=${QUOTADIR}/file2 bs=${file_size} count=1";echo "\tExec: ${cmd}"; eval ${cmd} &> /dev/null

    echo -n "\tbefore mv (ls $QUOTADIR):\t"; ls $QUOTADIR
    
    cmd="mv ${fromdir} ${QUOTADIR}/file2";echo "\tExec: ${cmd}"; eval ${cmd}; res=$?
    eval echo -n "==${test_num}==\${msg$test_num}===="
    if (($res == 1)); then echo "success\n"; else echo "fail\n"; exit 1; fi
}

echo "/************************************************************************"
echo "测试 rename/mv 目录==== 源和目标目录父目录相同"
echo "************************************************************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi
source ${QUOTA_COMMON_FUNC}

reset_all 10;

test_num=1; if (($1 == ${test_num})) || (($1 == 0));then rename_from_childdir_to_nonexistdir 3 4; fi
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then rename_from_childdir_to_existdir 6 2; fi
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcdir_to_destdir_includesrc; fi
#test_num=4; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcdir_to_destdir_includesrc_backup; fi
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then rename_from_srcdir_to_file; fi

echo "/******************************************************************************"
echo "测试 rename/mv 目录==== 源和目标目录父目录相同 all success"
echo "******************************************************************************/"
alias mv='mv -i' # 强制覆盖时需要确认
exit 0;